/**  
* @Title: MapUtil.java 
* @Description:
* @Copyright: Copyright (c) 2018
* @Company:http://www.sinocon.cn
* @author Administrator  
* @date 2018年3月27日  
* @version 1.0  
*/
package com.yuanlinsu.common;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.ObjectUtils;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * @Title: MapUtil
 * @Description:
 * @author:Administrator
 * @date 2018年3月27日
 */
public class MapUtil {
	private static final Log log = LogFactory.get();
	
	public static int ToInt(Object oArgu) {
		String sReturn = "";
		String sFlag = "";
		String sData = ToString(oArgu).trim();
		if (sData.length() > 1) {
			if (sData.charAt(0) == '-') {
				sData = sData.substring(1);
				sFlag = "Ok";
			}
		}
		for (int i = 0; i < sData.length(); i++) {
			if (Character.isDigit(sData.charAt(i)))
				sReturn = sReturn + sData.charAt(i);
			else
				break;
		}
		if (sReturn == "") {
			sReturn = "0";
		} else {
			if (sFlag == "Ok")
				sReturn = "-" + sReturn;
		}
		return Integer.parseInt(sReturn);
	}

	public static double ToDbl(Object oArgu) {
		double dReturn = 0;
		if (oArgu == null)
			return dReturn;
		String sValue = oArgu.toString().trim();
		if (sValue == "")
			sValue = "0";
		try {
			dReturn = Double.parseDouble(sValue);
		} catch (Exception err) {
			dReturn = 0;
		}
		return dReturn;
	}

	public static String ToString(Object oArgu) {
		if (oArgu == null)
			oArgu = "";
		return oArgu.toString();
	}

	public static Object getMapValue(String key, Map<String, Object> argmap) {
		if (!ObjectUtils.isEmpty(argmap)) {
			return argmap.get(key);
		}
		return null;
	}

	public static Map<String, Object> getParamMapForRequest(HttpServletRequest request) {
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.error("设置编码错误{}",e);
		}
		Map<String, Object> argMap = new HashMap<String, Object>();
		Map<String, String[]> parameterMap = request.getParameterMap();
		for (String key : parameterMap.keySet()) {
			String[] strings = parameterMap.get(key);
			argMap.put(key, strings[0]);
			if (strings.length > 1) {
				log.error("错误提醒：-----------提交的表单中重复赋值，请修正，后台只接收了一个---------");
			}
		}
		
		return argMap;
	}
}
